﻿using System;
using System.Collections;
using System.Configuration;
using BONSAI.Common;
using BONSAI.Common.DTO;
using System.Web.UI.WebControls;

namespace BONSAI.UI.Pages
{
    public partial class HomeCart : System.Web.UI.Page
    {
        public int Stt = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session[BONSAIConstants.SHOPPING_CART] != null)
                {
                    var cart = new ShoppingCart
                                   {
                                       gCart = (ArrayList)Session[BONSAIConstants.SHOPPING_CART]
                                   };
                    lblTotal.Text = String.Format("{0:#,###.##}", cart.getTotal());
                    grdCart.DataSource = cart.gCart;
                    grdCart.DataBind();


                    var ui = Guid.NewGuid().ToString().Substring(1, 5);
                    txtOrderId.Text = ui;

                }
                else {
                    Response.Redirect("~/home");
                }
                
            }
        }
        protected string  BindProductEcart()
        {

            var tmptable = "";

            tmptable = "<table  border='0' width='100%' cellspacing='0' cellpadding='0' id='cartContentsDisplay'> ";
            tmptable += "<tr class='tableHeading'> ";

            tmptable += "<th scope='col' id='scQuantityHeading'>Stt</th> ";
            tmptable += "<th scope='col' id='scProductsHeading'>Tên Sản Phẩm</th> ";
            tmptable += "<th scope='col' id='scTotalHeading'>S.Lượng</th>";
            tmptable += "<th scope='col' id='scTotalHeading'>Đơn Giá (VNĐ)</th> ";
            tmptable += "<th scope='col' id='scRemoveHeading'>Thành Tiền</th> ";
            tmptable += "</tr> ";
            if (Session[BONSAIConstants.SHOPPING_CART] == null)
            {
                return null;
            }
            var lstProductECartInfo = new ShoppingCart
            {
                gCart = (ArrayList)Session[BONSAIConstants.SHOPPING_CART]
            };
            var temp = 1;
            for (var i = 0; i < lstProductECartInfo.gCart.Count; i++)
            {
                tmptable += "<tr class='rowEven'> ";
                tmptable += "<td class='cartQuantity'> ";
                tmptable += temp++;
                tmptable += "</td> ";
                tmptable += "<td class='cartProductDisplay'>";
                tmptable += lstProductECartInfo.gProduct_name;
                tmptable += "</td> ";
                tmptable += "<td class='cartTotalDisplay'>";
                tmptable += lstProductECartInfo.gQuantity;
                tmptable += "</td>";
                tmptable += "<td class='cartTotalDisplay'>";
                tmptable += String.Format("{0:#,###}", lstProductECartInfo.gPrice);
                tmptable += "</td> ";
                tmptable += "<td class='cartTotalDisplay'>";
                tmptable += String.Format("{0:#,###}", lstProductECartInfo.gSubTotal);
                tmptable += "</td> ";
                tmptable += "</tr> ";
                
            }
            tmptable += "</table>";

            tmptable += "<div id='cartSubTotal'>";
            tmptable += "Tổng tiền : <strong>" + String.Format("{0:#,###}", lstProductECartInfo.getTotal()) + "</strong></div>";
           
            return tmptable;
        }
        protected void btnsendmail_Click(object sender, EventArgs e)
        {
            if (Session[BONSAIConstants.SHOPPING_CART] != null)
            {
                var customerName = txtfullname.Text;
                var address = txtAddress.Text;
                var phone = txtPhone.Text;
                var orderId = txtOrderId.Text;
                var product = BindProductEcart();
                var customerContent = txtContent.Text;

                if (string.IsNullOrEmpty(customerName))
                {
                    txtfullname.Focus();
                    return;
                }
                
                try
                {
                    //create mailInfo
                    var mailInfo = new OrderMailInfo
                    {
                        SystemDisplayName = ConfigurationManager.AppSettings[BONSAIConstants.COMPANY_NAME],
                        Message = (customerContent),
                        Address = address,
                        Name = customerName,
                        Tel = phone,
                        OrderId = orderId,
                        Product = product,
                        EmailTemplate =
                            Server.MapPath("~\\" +
                                           ConfigurationManager.AppSettings[
                                               BONSAIConstants.ORDERMAILTEMPLATE]),
                        EmailTo = ConfigurationManager.AppSettings[BONSAIConstants.COMPANY_EMAIL]
                    };

                    // send mail
                    BONSAIUtils.EmailUtil.SendOrderMail(mailInfo);
                    WebMBox.Show("Gởi mail thành công");
                    return;
                }
                catch (Exception)
                {
                    WebMBox.Show("Vui lòng xem lại các thông tin trên");
                    return;
                }
            }
            WebMBox.Show("Vui lòng chọn sản phẩm cần mua.");
        }

        protected void grdCart_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            var task = e.CommandName;
            var productId = e.CommandArgument.ToString();
            var cart = new ShoppingCart();
            if (string.IsNullOrEmpty(productId))
            {
                return;
            }
            switch (task)
            {
                case "updateCart":
                    if (Session[BONSAIConstants.SHOPPING_CART] != null)
                    {
                        cart.gCart = (ArrayList)Session[BONSAIConstants.SHOPPING_CART];
                    }
                    var count = 0;
                    TextBox lstQuantity = new TextBox();
                    lstQuantity = (TextBox)grdCart.Items[e.Item.ItemIndex].FindControl("txtQuantity");
                    try
                    {
                        if (lstQuantity != null) count = Int32.Parse(lstQuantity.Text);
                        else
                            WebMBox.Show("Vui lòng nhập lại số lượng");
                    }
                    catch (Exception)
                    {
                        WebMBox.Show("Vui lòng nhập lại số lượng");
                    }
                    cart.updateCart(productId, count);
                    Session[BONSAIConstants.SHOPPING_CART] = cart.gCart;
                    Response.Redirect("~/home-cart/");
                    break;

                case "deleteCart":
                    if (Session[BONSAIConstants.SHOPPING_CART] != null)
                    {
                        cart.gCart = (ArrayList)Session[BONSAIConstants.SHOPPING_CART];
                    }

                    cart.removeCart(productId);
                    Session[BONSAIConstants.SHOPPING_CART] = cart.gCart;
                    Response.Redirect("~/home-cart/");
                    break;
            }
        }
        
     }
}